package game.zqll.mod;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

// ModMeta 类，包含常见 mod.conf 字段
public class ModMeta {
    /**
     * Mod 唯一ID
     */
    public String modId;
    /**
     * Mod 名称
     */
    public String name;
    /**
     * Mod 版本
     */
    public String version;
    /**
     * Mod 主类
     */
    public String mainClass;
    /**
     * 作者
     */
    public String author;
    /**
     * 描述
     */
    public String description;
    /**
     * 官网
     */
    public String homepage;
    /**
     * 支持的游戏版本
     */
    public String gameVersion;
    /**
     * 支持的多个游戏版本（可选）
     */
    public List<String> compatibleGameVersions;
    /**
     * 依赖列表
     */
    public List<Dependency> dependencies;
    /**
     * 冲突列表
     */
    public List<Conflict> conflicts;
    /**
     * 标签
     */
    public List<String> tags;

    // 读取 mod.conf 并返回 ModMeta 对象
    public static ModMeta readFromJar(String jarPath) throws Exception {
        try (JarFile jarFile = new JarFile(jarPath)) {
            // 支持 mod.conf 或 mod.conf.json
            ZipEntry entry = jarFile.getEntry("mod.conf");
            if (entry == null) {
                entry = jarFile.getEntry("mod.conf.json");
            }
            if (entry == null) {
                throw new RuntimeException("mod.conf 或 mod.conf.json 未找到: " + jarPath);
            }
            try (InputStream is = jarFile.getInputStream(entry)) {
                Config config = ConfigFactory.parseReader(new java.io.InputStreamReader(is, java.nio.charset.StandardCharsets.UTF_8));
                return ModMeta.fromConfig(config);
            }
        }
    }

    public static ModMeta fromConfig(Config config) {
        ModMeta meta = new ModMeta();
        meta.modId = config.hasPath("modId") ? config.getString("modId") : null;
        meta.name = config.hasPath("name") ? config.getString("name") : null;
        meta.version = config.hasPath("version") ? config.getString("version") : null;
        meta.mainClass = config.hasPath("mainClass") ? config.getString("mainClass") : null;
        meta.author = config.hasPath("author") ? config.getString("author") : null;
        meta.description = config.hasPath("description") ? config.getString("description") : null;
        meta.homepage = config.hasPath("homepage") ? config.getString("homepage") : null;
        meta.gameVersion = config.hasPath("gameVersion") ? config.getString("gameVersion") : null;
        meta.compatibleGameVersions = config.hasPath("compatibleGameVersions")
            ? config.getStringList("compatibleGameVersions") : null;
        meta.tags = config.hasPath("tags") ? config.getStringList("tags") : null;

        // 依赖
        meta.dependencies = new ArrayList<>();
        if (config.hasPath("dependencies")) {
            for (Config depConf : config.getConfigList("dependencies")) {
                Dependency dep = new Dependency();
                dep.modId = depConf.hasPath("modId") ? depConf.getString("modId") : null;
                dep.version = depConf.hasPath("version") ? depConf.getString("version") : null;
                meta.dependencies.add(dep);
            }
        }

        // 冲突
        meta.conflicts = new ArrayList<>();
        if (config.hasPath("conflicts")) {
            for (Config confConf : config.getConfigList("conflicts")) {
                Conflict conf = new Conflict();
                conf.modId = confConf.hasPath("modId") ? confConf.getString("modId") : null;
                meta.conflicts.add(conf);
            }
        }
        return meta;
    }

    public static class Dependency {
        public String modId;
        public String version;
    }

    public static class Conflict {
        public String modId;
    }

    @Override
    public String toString() {
        return "ModMeta{" +
            "modId='" + modId + '\'' +
            ", name='" + name + '\'' +
            ", version='" + version + '\'' +
            ", mainClass='" + mainClass + '\'' +
            ", author='" + author + '\'' +
            ", description='" + description + '\'' +
            ", homepage='" + homepage + '\'' +
            ", gameVersion='" + gameVersion + '\'' +
            ", compatibleGameVersions=" + compatibleGameVersions +
            ", dependencies=" + dependencies +
            ", conflicts=" + conflicts +
            ", tags=" + tags +
            '}';
    }
}
